package com.sptpc.learnspring.global.response;

import lombok.*;

import java.io.Serializable;

/**
 * 统一的REST API响应封装
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class GlobalResponse implements Serializable {
    // 状态码
    private Integer code;
    // 返回消息
    private String msg;
    // 返回数据
    private Object data;

    /**
     * 带数据的成功返回
     */
    public static GlobalResponse success(Object data) {
        return GlobalResponse
                .builder()
                .code(BusinessStatusCode.SUCCESS.getCode())
                .msg(BusinessStatusCode.SUCCESS.getMessage())
                .data(data)
                .build();
    }

    /**
     * 无数据的成功返回,只携带消息
     */
    public static GlobalResponse success() {
        return success(null);
    }

    /**
     * 指定ResultCode的失败返回
     */
    public static GlobalResponse fail(BusinessStatusCode resultCode) {
        return GlobalResponse
                .builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMessage())
                .data(null)
                .build();
    }
}
